package com.micangcaihang.common.base.fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.micangcaihang.common.base.app.BaseApp;
import com.micangcaihang.common.base.app.IContext;
import com.micangcaihang.common.baserx.RxManager;

import butterknife.ButterKnife;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayonggmail@gmail.com
 *     time   : 2017/06/20
 *     desc   : Fragment基类
 *     version: 1.0
 * </pre>
 */

public abstract class BaseDataBingFragment<B extends ViewDataBinding> extends SupportFragment implements IContext {

    public View rootView;

    public LayoutInflater inflater;
    /**
     * Preseter对象
     */
    public B mViewBinding;
    public RxManager mRxManager;

    private Bundle mySavedInstanceState;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        if (rootView == null) {
            mViewBinding = DataBindingUtil.inflate(inflater, this.getLayoutId(), container, false);
            rootView = mViewBinding.getRoot();
            ButterKnife.bind(this, rootView);
            beforeInitView(savedInstanceState);
            initView(rootView);
            initPresenter();
            initData();
            initListener();
        } else {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mRxManager != null) {
            mRxManager.clear();
        }
    }

    /**
     * @param savedInstanceState
     */
    protected void beforeInitView(Bundle savedInstanceState) {
        mRxManager = new RxManager();
        this.mySavedInstanceState = savedInstanceState;
    }

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    protected abstract int getLayoutId();

    /**
     * 初始化view
     *
     * @param rootView 资源布局view
     */
    public abstract void initView(View rootView);

    /**
     * 初始化对象
     */
    protected abstract void initData();

    /**
     * 简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
     */
    public void initPresenter() {

    }

    /**
     * 初始化按钮监听
     */
    protected abstract void initListener();

    /*IContext interface start*/
    @Override
    public <T extends BaseApp> T getAppContext() {
        return (T) BaseApp.me();
    }
    /*IContext interface end*/

    /**
     * [页面跳转]
     *
     * @param clz
     */
    public void startActivity(Class<?> clz) {
        startActivity(new Intent(_mActivity, clz));
    }

    /**
     * 携带数据页面跳转
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(_mActivity, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * startActivityForResult
     *
     * @param clazz       目标Activity
     * @param requestCode 发送判断值
     */
    protected void startActivityForResult(Class<?> clazz, int requestCode) {
        Intent intent = new Intent(_mActivity, clazz);
        startActivityForResult(intent, requestCode);
    }

    /**
     * startActivityForResult with bundle
     *
     * @param clazz       目标Activity
     * @param requestCode 发送判断值
     * @param bundle      数据
     */
    protected void startActivityForResult(Class<?> clazz, int requestCode, Bundle bundle) {
        Intent intent = new Intent(_mActivity, clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }
}
